<template>
	<view style="display: flex;flex-direction: column;align-items: center;">

		<view v-for="(item,index) in noteListObj.list" class="noteContainer" :key="index">
			<view class="header" @click="gotoNoteDetail(item.id)">
				<view class="left">
					<view class="title text-2line-ellipsis">
						{{item.title}}
					</view>
					<view class="detail text-2line-ellipsis">
						{{item.briefly}}
					</view>

					<view class="dataWrapper">
						<view class="pageViews">
							<image class="iconWrapper" src="@/static/image/icon_preview.png" mode=""></image>
							{{item.pageViews||"0"}}
						</view>
						<view class="timeWrapper">
							<image class="iconWrapper" src="@/static/image/icon_time.png" mode=""></image>
							{{item.updateTime||item.createTime}}
						</view>
					</view>
				</view>
				<image class="right" :src="item.cover" mode="scaleToFill"></image>
			</view>
			<view class="toolWrapper">
				<view class="toolItem" @click="gotoNoteStatistic(item.id)">
					<image class="icon" src="@/static/image/icon_note_data.png" mode=""></image>
					数据
				</view>
				<view class="line">

				</view>
				<view class="toolItem" @click="onShare({
					id: item.id,
					title: item.title,
					sharePath: '/pages/client/note/detail?id=' + item.id,
					imageUrl: item.cover,
				})">
					<image class="icon" src="@/static/image/icon_note_share.png" mode=""></image>
					分享
				</view>
				<view class="line">

				</view>
				<view class="toolItem" @click="onSetCover(item)">
					<image class="icon" src="@/static/image/icon_note_cover.png" mode=""></image>
					封面
				</view>
				<view class="line">

				</view>
				<view class="toolItem" @click="onSetMore(item)"> 
					<image class="icon" src="@/static/image/icon_note_more.png" mode=""></image>
					更多
				</view>
			</view>
		</view>
		<view class="" style="height: 200rpx;">

		</view>

		<popupUploadImage v-model="showCoverPopup" @onSuccess="onUploadCover">
		</popupUploadImage>
		
		<popupMore v-model="showMorePopup" :noteObj="selectNoteObj" 	
			@refresList="onRefreshList">
		</popupMore>

		<view class="bottomBtnContainer">
			<view class="bottomBtnWrapper" @click="gotoNoteDetail()">
				+ 新增笔记
			</view>
		</view>

	</view>
</template>

<script>
	import {
		mapState,
		mapMutations,
		mapActions
	} from 'vuex';

	let that = null
	const api = require("@/static/js/api.js")
	const utils = require("@/static/js/utils.js")

	import popupUploadImage from "@/pages/shop/components/popupUploadImage/index.vue"
	import popupMore from "./subcomponent/popupMore.vue"
	export default {
		components: {
			popupUploadImage,
			popupMore
		},
		computed: {
			...mapState({
				hasLogin: state => state.userInfo.hasLogin,
				isAuthUserInfo: state => state.userInfo.isAuthUserInfo,
				isAuthPhone: state => state.userInfo.isAuthPhone,
				isSeller: state => state.userInfo.isSeller,
				avatarUrl: state => state.userInfo.avatarUrl,
				nickName: state => state.userInfo.nickName,
				telephone: state => state.userInfo.telephone,
				openId: state => state.userInfo.openId,
				employeeId: state => state.userInfo.employeeId,
			}),
		},

		data() {
			return {
				noteListObj: {
					list: [],
					loadStatus: "loading",
					pageIndex: 1,
					curPageIndex: 0,
				},
				selectNoteObj: {},
				showCoverPopup: false,
				showMorePopup: false,
			};
		},

		onLoad(options) {
			that = this;
		},
		onShow() {
			that.clearListShow(that.noteListObj)
			that.getNoteList()
		},


		onReachBottom() {
			that.getNoteList()
		},

		methods: {
			onRefreshList(){
				that.clearListShow(that.noteListObj)
				that.getNoteList()
			},
			onSetCover(item) {
				that.selectNoteObj = item
				that.showCoverPopup = true
			},
			onSetMore(item) {
				that.selectNoteObj = item
				that.showMorePopup = true
			},
			
			onUploadCover(path) {
				console.log("onUploadCover", path)
				api.getNoteUpate({
					param: {
						id: that.selectNoteObj.id,
						cover: path,
					}
				}).then(res => {
					console.log("createNote", res)
					if (res.success) {
						that.clearListShow(that.noteListObj)
						that.getNoteList()
						uni.showToast({
							icon: "none",
							title: "保存成功"
						})
					}
				}).finally(() => {
					that.doingSave = false
				})
			},

			gotoNoteDetail(id) {
				if (id) {
					uni.navigateTo({
						url: "./createNote?id=" + id
					})
				} else {
					uni.navigateTo({
						url: "/pages/shop/note/createNote"
					})
				}

			},
			
			gotoNoteStatistic(id) {
				if (id) {
					uni.navigateTo({
						url: "./noteStatistic?id=" + id
					})
				}
			},
			
			getNoteList() {
				if (that.noteListObj.curPageIndex != that.noteListObj.pageIndex) {
					that.noteListObj.curPageIndex = that.noteListObj.pageIndex
					api.getNoteList({
						param: {
							limit: 10,
							page: that.noteListObj.pageIndex,
							title: that.articleSearchTitle,
						}
					}).then(res => {
						console.log(res)
						if (res.code == 0) {
							that.pushListShow(that.noteListObj, res.data)
						}
					})
				}

			},


			/**
			 * @description: 列表中添加数据
			 * @param {number} id   u-tabs-swiper的当前ID
			 * @param {list}  列表
			 * @return void
			 */
			pushListShow(srcObj, pushlist) {
				if (pushlist && pushlist.length > 0) {
					for (let i = 0; i < pushlist.length; i++) {
						// pushlist[i].createTime = utils.formatTime(pushlist[i].createTime, 'yyyy-MM-dd')
						srcObj.list.push(pushlist[i])
					}
					srcObj.pageIndex++;

					if (pushlist.length == 10) {
						srcObj.loadStatus = "loadmore"
					} else {
						srcObj.loadStatus = "nomore"
					}
				} else {
					srcObj.loadStatus = "nomore"
				}
			},

			//----清空列表
			clearListShow(srcObj) {
				srcObj.curPageIndex = 0;
				srcObj.pageIndex = 1;
				srcObj.list.splice(0, srcObj.list.length)
			},

		}
	}
</script>

<style>
	page {
		background-color: #f8f8f8;
	}
</style>
<style lang="scss">
	.noteContainer {
		width: 714rpx;
		background: #FFFFFF;
		box-shadow: 2rpx 2rpx 2rpx 1rpx #FFFFFF;
		border-radius: 20rpx;
		padding: 30rpx 30rpx 0 30rpx;
		margin-top: 30rpx;

		.header {
			width: 100%;
			display: flex;
			justify-content: space-between;
			align-items: center;
			.left {
				width: 454rpx;
				height: 100%;
				display: flex;
				flex-direction: column;
				justify-content: space-between;

				.title {
					width: 100%;
					font-size: 30rpx;
					color: #515151;
					line-height: 42rpx;
					font-weight: bold;
					margin-bottom: 10rpx;
				}

				.detail {
					width: 100%;
					font-size: 22rpx;
					line-height: 40rpx;
					color: rgba(25, 25, 25, 0.5);
					min-height: 50rpx;
					margin-bottom: 20rpx;
				}

				.dataWrapper {
					display: flex;
					width: 100%;
					justify-content: space-between;
					align-items: center;
					color: rgba(60, 60, 60, 0.5);
					font-size: 20rpx;
					margin-bottom: 15rpx;

					.pageViews {
						display: flex;
						align-items: center;

						.iconWrapper {
							width: 28rpx;
							height: 16rpx;
							margin-right: 10rpx;
						}
					}

					.timeWrapper {
						display: flex;
						align-items: center;

						.iconWrapper {
							width: 19rpx;
							height: 18rpx;
							margin-right: 10rpx;
						}
					}
				}
			}

			.right {
				width: 184rpx;
				height: 138rpx;
				border-radius: 20rpx;
			}
		}

		.toolWrapper {
			border-top: 1px solid #ececec;
			width: 100%;
			height: 80rpx;
			display: flex;
			justify-content: space-around;
			align-items: center;

			.toolItem {
				display: flex;
				align-items: center;
				font-size: 23rpx;
				color: #686868;

				.icon {
					width: 31rpx;
					height: 26rpx;
					margin-right: 10rpx;
				}
			}

			.line {
				width: 1px;
				height: 38rpx;
				background-color: #d8d8d8;
			}
		}
	}

</style>